package com.bwie.bwedu.presenter.impl;

import android.text.TextUtils;

import com.bwie.bwedu.activity.StudentInfoActivity;
import com.bwie.bwedu.bean.Student;
import com.bwie.bwedu.model.AddStuListen;
import com.bwie.bwedu.model.impl.CommitStuImpl;
import com.bwie.bwedu.model.impl.SaveLocalStuImpl;
import com.bwie.bwedu.presenter.AddStuInfoPresenter;
import com.bwie.bwedu.utils.PhoneUtil;
import com.bwie.bwedu.utils.ToastUtils;

/**
 * Created by 沫沫~ on 2016/7/23.
 */
public class AddStuImpl implements AddStuInfoPresenter,AddStuListen {

	private StudentInfoActivity studentInfoActivity;
	private CommitStuImpl commitStu;
	private SaveLocalStuImpl saveLocalStu;
	private boolean b;

	public AddStuImpl(StudentInfoActivity studentInfoActivity) {
		this.studentInfoActivity = studentInfoActivity;
		commitStu = new CommitStuImpl();
		saveLocalStu = new SaveLocalStuImpl();
	}

	@Override
	public void submit(String per, String ptel, String stu, String stel, String adress, String IDcard, String id, String procurator) {

		if (TextUtils.isEmpty(per)&&TextUtils.isEmpty(ptel)&&TextUtils.isEmpty(adress)&&TextUtils.isEmpty(id)&&TextUtils.isEmpty(procurator)||TextUtils.isEmpty(stu)&&TextUtils.isEmpty(stel)&&TextUtils.isEmpty(adress)&&TextUtils.isEmpty(id)&&TextUtils.isEmpty(procurator)){
			ToastUtils.getToast("请填写有效信息");
			return;
		}else if (ptel.length()!=11){
			ToastUtils.getToast("请输入11位手机号");
			return;
		} else {
			commitStu.Comit(per,ptel,stu,stel,adress,IDcard,id,procurator,this);
		}

		if (TextUtils.isEmpty(per) && TextUtils.isEmpty(ptel) && TextUtils.isEmpty(stu) && TextUtils.isEmpty(stel)){
			b = true;
		}

		if (!TextUtils.isEmpty(adress) && !TextUtils.isEmpty(procurator)){

			if (!TextUtils.isEmpty(per) || !TextUtils.isEmpty(ptel)){
				String st = "";
				if (TextUtils.isEmpty(per)){
					st = st+"联系人,";
				}
				if (TextUtils.isEmpty(ptel)){
					st = st+"电话,";
				}
				if(TextUtils.isEmpty(adress)){
					st = st+"地址,";
				}
				if (TextUtils.isEmpty(procurator)){
					st = st+"代理人,";
				}
				if (st.length()>0){
					ToastUtils.getToast(st.substring(0,st.length()-1)+"为空");
				}

			}

			if (!TextUtils.isEmpty(adress) && !TextUtils.isEmpty(procurator)){
				if (b){
					ToastUtils.getToast("请输入姓名和联系方式");
				}
			}
			if (!TextUtils.isEmpty(stu) || !TextUtils.isEmpty(stel)){
				String st = "";
				if (TextUtils.isEmpty(stu)){
					st = st+"学员姓名,";
				}
				if (TextUtils.isEmpty(stel)){
					st = st+"电话,";
				}
				if(TextUtils.isEmpty(adress)){
					st = st+"地址,";
				}
				if (TextUtils.isEmpty(procurator)){
					st = st+"代理人,";
				}
				if (st.length()>0){
					ToastUtils.getToast(st.substring(0,st.length()-1)+"为空");
				}

			}
		}else {
			ToastUtils.getToast("请填写其他信息");
		}

	}

	public void submit(Student student) {
		String per=student.gxxm;
		String ptel=student.gxdh;
		String adress=student.jg;
		String id=student.xb;
		String procurator=student.dlrid;
		String stu=student.xm;
		String stel=student.grsj;
		if (TextUtils.isEmpty(stu)){
			if (TextUtils.isEmpty(per)){
				studentInfoActivity.onFail("学生及联系人为空");
				return;
			}else if (TextUtils.isEmpty(ptel)){
				studentInfoActivity.onFail("请输入联系人电话");
				return;
			}
			else if (!PhoneUtil.isMobileNO(ptel)){
				studentInfoActivity.onFail("请输入合法联系人电话");
				return;
			}
		}else if (TextUtils.isEmpty(stel)){
			if (TextUtils.isEmpty(student.gxxm)||TextUtils.isEmpty(student.gxdh)){
				studentInfoActivity.onFail("请输入学生电话");
				return;
			}
		}
		if (!TextUtils.isEmpty(student.grsj)&&!student.grsj.equals("null")&&!PhoneUtil.isMobileNO(student.grsj)){
			studentInfoActivity.onFail("请输入合法学生电话");
			return;
		}
		if (TextUtils.isEmpty(adress)){
			studentInfoActivity.onFail("请选择所在地");
			return;
		}
		if (TextUtils.isEmpty(id)){
			studentInfoActivity.onFail("请选择性别");
			return;
		}
		if (TextUtils.isEmpty(procurator)){
			studentInfoActivity.onFail("请选择代理人");
			return;
		}
		commitStu.Comit(student,this);

//		if (TextUtils.isEmpty(per)&&TextUtils.isEmpty(ptel)&&TextUtils.isEmpty(adress)&&TextUtils.isEmpty(id)&&TextUtils.isEmpty(procurator)||TextUtils.isEmpty(stu)&&TextUtils.isEmpty(stel)&&TextUtils.isEmpty(adress)&&TextUtils.isEmpty(id)&&TextUtils.isEmpty(procurator)){
//			ToastUtils.getToast("请填写有效信息");
//			return;
//		}else if (ptel.length()!=11){
//			ToastUtils.getToast("请输入11位手机号");
//			return;
//		}else{
//			commitStu.Comit(student);
//		}
//
//		if (TextUtils.isEmpty(per) && TextUtils.isEmpty(ptel) && TextUtils.isEmpty(stu) && TextUtils.isEmpty(stel)){
//			b = true;
//		}
//
//		if (!TextUtils.isEmpty(adress) && !TextUtils.isEmpty(procurator)){
//
//			if (!TextUtils.isEmpty(per) || !TextUtils.isEmpty(ptel)){
//				String st = "";
//				if (TextUtils.isEmpty(per)){
//					st = st+"联系人,";
//				}
//				if (TextUtils.isEmpty(ptel)){
//					st = st+"电话,";
//				}
//				if(TextUtils.isEmpty(adress)){
//					st = st+"地址,";
//				}
//				if (TextUtils.isEmpty(procurator)){
//					st = st+"代理人,";
//				}
//				if (st.length()>0){
//					ToastUtils.getToast(st.substring(0,st.length()-1)+"为空");
//				}
//
//			}
//
//			if (!TextUtils.isEmpty(adress) && !TextUtils.isEmpty(procurator)){
//				if (b){
//					ToastUtils.getToast("请输入姓名和联系方式");
//				}
//			}
//			if (!TextUtils.isEmpty(stu) || !TextUtils.isEmpty(stel)){
//				String st = "";
//				if (TextUtils.isEmpty(stu)){
//					st = st+"学员姓名,";
//				}
//				if (TextUtils.isEmpty(stel)){
//					st = st+"电话,";
//				}
//				if(TextUtils.isEmpty(adress)){
//					st = st+"地址,";
//				}
//				if (TextUtils.isEmpty(procurator)){
//					st = st+"代理人,";
//				}
//				if (st.length()>0){
//					ToastUtils.getToast(st.substring(0,st.length()-1)+"为空");
//				}
//
//			}
//		}else {
//			ToastUtils.getToast("请填写其他信息");
//		}

	}


	@Override
	public void saveLocal(String per, String ptel, String stu, String stel, String adress, String IDcard, String id, String procurator) {
		saveLocalStu.SaveLocal(per,ptel,stu,stel,adress,IDcard,id,procurator,this);
	}

	public void saveLocal(Student student) {
		String per=student.gxxm;
		String ptel=student.gxdh;
		String adress=student.jg;
		String id=student.xb;
		String procurator=student.dlrid;
		String stu=student.xm;
		String stel=student.grsj;
		if (TextUtils.isEmpty(student.xm)){
			if (TextUtils.isEmpty(student.gxxm)){
				studentInfoActivity.onFail("学生及联系人为空");
				return;
			}else if (TextUtils.isEmpty(student.gxdh)){
				studentInfoActivity.onFail("请输入联系人电话");
				return;
			}
			else if (!PhoneUtil.isMobileNO(student.gxdh)){
				studentInfoActivity.onFail("请输入合法联系人电话");
				return;
			}
		}else if (TextUtils.isEmpty(student.grsj)){
			if (TextUtils.isEmpty(student.gxxm)||TextUtils.isEmpty(student.gxdh)){
				studentInfoActivity.onFail("请输入学生电话");
				return;
			}
		}
		if (!TextUtils.isEmpty(student.grsj)&&!student.grsj.equals("null")&&!PhoneUtil.isMobileNO(student.grsj)){
			studentInfoActivity.onFail("请输入合法学生电话");
			return;
		}

		saveLocalStu.SaveLocal(student,this);
	}



	@Override
	public void onSuccess(String id) {
		studentInfoActivity.onSuccess(id);
	}

	@Override
	public void onFail(String info) {
		studentInfoActivity.onFail(info);
	}
}
